package org.seven.jrdp.biz.t.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.seven.jrdp.biz.t.mapper.TCkdMapper;
import org.seven.jrdp.biz.t.mapper.TCkdMxMapper;
import org.seven.jrdp.biz.t.mapper.TCrkMapper;
import org.seven.jrdp.biz.t.mapper.TKqMxMapper;
import org.seven.jrdp.biz.t.model.TCkd;
import org.seven.jrdp.biz.t.model.TCkdMx;
import org.seven.jrdp.biz.t.model.TCrk;
import org.seven.jrdp.biz.t.model.THt;
import org.seven.jrdp.biz.t.model.TJh;
import org.seven.jrdp.biz.t.model.TKh;
import org.seven.jrdp.biz.t.model.TKqMx;
import org.seven.jrdp.biz.t.model.TWl;
import org.seven.jrdp.biz.t.model.TZz;
import org.seven.jrdp.commons.config.ParamConfig;
import org.seven.jrdp.commons.exception.BizException;
import org.seven.jrdp.commons.mybatis.model.PageRequest;
import org.seven.jrdp.commons.util.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class TCkdService {
	@Autowired
	private TKqMxMapper tKqMxMapper;
	@Autowired
	private TCkdMapper tCkdMapper;
	@Autowired
	private TCkdMxMapper tCkdMxMapper;
	@Autowired
	private TCrkMapper tCrkMapper;
	@Autowired
	private TKqMxService tKqMxService;
	@Autowired
	private THtService tHtService;
	@Autowired
	private TKhService tKhService;
	@Autowired
	private TJhService tJhService;
	@Autowired
	private TZzService tZzService;
	@Autowired
	private TWlService tWlService;

	@Transactional(readOnly = true)
	public PageInfo<Map<String, Object>> find(Map<String, Object> map, PageRequest pageRequest) {
		PageHelper.startPage(pageRequest.getPage(), pageRequest.getRows());
		return new PageInfo<Map<String, Object>>(tCkdMapper.find(map));
	}

	@Transactional(readOnly = true)
	public PageInfo<Map<String, Object>> findQxck(Map<String, Object> map, PageRequest pageRequest) {
		PageHelper.startPage(pageRequest.getPage(), pageRequest.getRows());
		return new PageInfo<Map<String, Object>>(tCkdMapper.findQxck(map));
	}

	@Transactional(readOnly = true)
	public PageInfo<Map<String, Object>> findFhd(Map<String, Object> map, PageRequest pageRequest) {
		PageHelper.startPage(pageRequest.getPage(), pageRequest.getRows());
		List<Map<String, Object>> list = tCkdMapper.findFhd(map);
		for (Map<String, Object> fhd : list) {
			List<String> lh = tCkdMapper.findFhdMx((String) fhd.get("id"));
			fhd.put("lh", lh);
		}
		return new PageInfo<Map<String, Object>>(list);
	}

	@Transactional(readOnly = true)
	public List<Map<String, Object>> findDc(Map<String, Object> map) {
		List<Map<String, Object>> list = tCkdMapper.findFhd(map);
		for (Map<String, Object> fhd : list) {
			List<String> lh = tCkdMapper.findFhdMx((String) fhd.get("id"));
			fhd.put("lh", lh);
		}
		return list;
	}

	@Transactional(readOnly = true)
	public TCkd get(String id) {
		return tCkdMapper.selectByPrimaryKey(id);
	}

	@Transactional(readOnly = true)
	public Map<String, Object> create(String ggxh, int ckzl) {
		boolean flag = false;
		int zlwc = ParamConfig.getZlwc();
		int min = ckzl * 1000 - zlwc;
		int max = ckzl * 1000 + zlwc;
		int yckzl = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> list = new ArrayList<String>();
		List<Map<String, Object>> mx = new ArrayList<Map<String, Object>>();
		map.put("mx", mx);
		List<Map<String, Object>> mxs = tCkdMapper.findCkmx(ggxh, ParamConfig.getCkhs());
		if ("AD10.1".equals(ggxh)) {
			for (int i = 0; i < mxs.size(); i += 2) {
				Map<String, Object> mx1 = mxs.get(i);
				Map<String, Object> mx2 = mxs.get(i + 1);
				mx.add(mx1);
				mx.add(mx2);
				list.add((String) mx1.get("cpbh") + "-" + (String) mx1.get("cpxh"));
				list.add((String) mx2.get("cpbh") + "-" + (String) mx2.get("cpxh"));
				yckzl += (int) mx1.get("zl") + (int) mx2.get("zl");
				if (yckzl >= min && yckzl <= max) {
					flag = true;
					break;
				}
			}
		} else {
			for (int i = 0; i < mxs.size(); i++) {
				Map<String, Object> mx1 = mxs.get(i);
				mx.add(mx1);
				list.add((String) mx1.get("cpbh") + "-" + (String) mx1.get("cpxh"));
				yckzl += (int) mx1.get("zl");
				if (yckzl >= min && yckzl <= max) {
					flag = true;
					break;
				}
			}
		}
		if (!list.isEmpty()) {
			List<Map<String, Object>> hz = tCkdMapper.findCkhz(list);
			map.put("hz", hz);
		}
		map.put("flag", flag);
		return map;
	}

	@Transactional
	public void insert(TCkd tCkd, List<TCrk> tCrks, List<TCkdMx> tCkdMxs) {
		Integer yckzl = tHtService.getYckzl(tCkd.getHtId());
		if (yckzl == null) {
			throw new BizException("未查询到已出库重量");
		}
		tCkd.setYckzl(yckzl);
		tCkd.setCkzt("00");
		int mx = tCkdMapper.getCountMx(DateUtils.toCalendar(tCkd.getCkrq()).get(Calendar.YEAR));
		tCkd.setMx("y" + mx);
		tCkdMapper.insertSelective(tCkd);
		tHtService.add(tCkd.getHtId(), tCkd.getCkzl());
		for (TCrk tCrk : tCrks) {
			tCrk.setCkId(tCkd.getId());
			tCrk.setRksj(DateTimeUtils.getDate(tCrk.getRksj1(), "yyyy-MM-dd HH:mm:ss.SSS"));
			tCrkMapper.insertSelective(tCrk);
		}
		for (TCkdMx tCkdMx : tCkdMxs) {
			tCkdMx.setId(tCkd.getId());
			tCkdMx.setRksj(DateTimeUtils.getDate(tCkdMx.getRksj1(), "yyyy-MM-dd HH:mm:ss.SSS"));
			tCkdMxMapper.insertSelective(tCkdMx);
			TKqMx tKqMx = new TKqMx();
			tKqMx.setCpbh(tCkdMx.getCpbh());
			tKqMx.setCpxh(tCkdMx.getCpxh());
			tKqMx.setCkId(tCkd.getId());
			tKqMxMapper.updateByPrimaryKeySelective(tKqMx);
		}
	}

	@Transactional
	public void update(TCkd tCkd) {
		tCkdMapper.updateByPrimaryKeySelective(tCkd);
	}

	@Transactional
	public void delete(String id) {
		TCkd tCkd = get(id);
		if (tCkd.getCkzt().equals("10") || tCkd.getCkzt().equals("20") || tCkd.getCkzt().equals("30")) {
			throw new BizException("只允许删除计划中的出库单");
		}
		String maxId = tCkdMapper.getMaxId();
		if (id.equals(maxId)) {
			tCkdMapper.deleteByPrimaryKey(id);
			tCkdMapper.deleteCkdMx(id);
			tCkdMapper.deleteCrk(id);
			tCkdMapper.updateKqMx(id);
			tHtService.subtract(tCkd.getHtId(), tCkd.getCkzl());
		} else {
			throw new BizException("只允许删除最新的出库单");
		}
	}

	@Transactional
	public void qxck(String id) {
		TCkd tCkd = get(id);
		if (tCkd.getCkzt().equals("20")) {
			tCkd.setCkzt("30");
			tCkdMapper.updateByPrimaryKeySelective(tCkd);
			tHtService.subtract(tCkd.getHtId(), tCkd.getCkzl());
		} else {
			throw new BizException("只允许取消已完成的出库单");
		}
	}

	@Transactional
	public void wcck(String id, String ckry) {
		Example example = new Example(TCkdMx.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", id);
		criteria.andIsNull("ckry");
		List<TCkdMx> list = tCkdMxMapper.selectByExample(example);
		for (TCkdMx tCkdMx : list) {
			TKqMx tKqMx = new TKqMx();
			tKqMx.setCpbh(tCkdMx.getCpbh());
			tKqMx.setCpxh(tCkdMx.getCpxh());
			tKqMx = tKqMxMapper.selectByPrimaryKey(tKqMx);
			tKqMxService.getCpck(tKqMx.getEwm());
			cpck(tKqMx.getEwm(), ckry);
		}
	}

	@Transactional
	public void cpck(String ewm, String ckry) {
		Map<String, Object> map = tKqMxService.getCpck(ewm);
		TKqMx tKqMx = new TKqMx();
		tKqMx.setCpbh((String) map.get("cpbh"));
		tKqMx.setCpxh((String) map.get("cpxh"));
		tKqMxMapper.deleteByPrimaryKey(tKqMx);
		tKqMxMapper.updateKqzt();
		TCkd tCkd = new TCkd();
		tCkd.setId((String) map.get("ckId"));
		tCkd.setUpdateBy(ckry);
		Example example = new Example(TKqMx.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("ckId", tCkd.getId());
		if (tKqMxMapper.selectCountByExample(example) > 0) {
			tCkd.setCkzt("10");
		} else {
			tCkd.setCkzt("20");
		}
		tCkdMapper.updateByPrimaryKeySelective(tCkd);
		TCkdMx tCkdMx = new TCkdMx();
		tCkdMx.setId((String) map.get("ckId"));
		tCkdMx.setCpbh((String) map.get("cpbh"));
		tCkdMx.setCpxh((String) map.get("cpxh"));
		tCkdMx.setCkry(ckry);
		tCkdMx.setCksj(DateTimeUtils.getDate());
		tCkdMxMapper.updateByPrimaryKeySelective(tCkdMx);
	}

	@Transactional(readOnly = true)
	public Map<String, Object> getDy(String id) {
		Map<String, Object> map = new HashMap<>();
		TCkd tCkd = get(id);
		map.put("ckd", tCkd);
		THt tHt = tHtService.get(tCkd.getHtId());
		map.put("ht", tHt);
		TKh tKh = tKhService.get(tHt.getKhId());
		map.put("kh", tKh);
		TJh tJh = tJhService.get(tHt.getJhId());
		map.put("jh", tJh);
		TZz tZz = tZzService.get(tHt.getZzId());
		map.put("zz", tZz);
		TWl tWl = tWlService.get(tCkd.getWlId());
		map.put("wl", tWl);
		List<Map<String, Object>> list = tCrkMapper.findHwmx(id);
		map.put("list", list);
		int yckzl = tCkdMapper.getYckzl(id);
		map.put("yckzl", yckzl);
		return map;
	}

	@Transactional(readOnly = true)
	public Map<String, Object> getDyMx(String id) {
		Map<String, Object> map = tCkdMapper.getDyMx(id);
		List<Map<String, Object>> list = tCkdMapper.findDyMx(id);
		map.put("list", list);
		List<Map<String, Object>> hzs = tCkdMapper.findDyMxHz(id);
		for (Map<String, Object> hz : hzs) {
			map.put((String) hz.get("cpbh"), hz);
		}
		return map;
	}
}